<?php
class Lib extends Controller{
	public function getImages($id){
		$doset=DataObject::get_by_id("Image",$id);
		return $doset?$doset:false;
	}
	function dropdown($class,$filter=null,$name,$id,$namelable,$showall=false){
		$Object = DataObject::get($class,$filter);//láº¥y dá»¯ liá»‡u
		if($Object)
		{
			if($showall)
			$Object = $Object->toDropdownMap('ID', $name, '...All... ', true);
			else
			$Object = $Object->toDropdownMap('ID', $name);
		}
		else
		{
			$Object=array('No data');
		}
		return new DropdownField($id,$namelable,$Object);
	}
	function dropdownLang($id,$namelable,$labelNone='All',$value='') {
		$Object=array(''=>$labelNone,'vi'=>'VietNam','en'=>'English');
		return new DropdownField($id,$namelable,$Object,$value);
	}
	function dropdownYesNo($id,$namelable) {
		$Object=array(''=>'All','0'=>'No','1'=>'Yes');
		return new DropdownField($id,$namelable,$Object);
	}
	function Whole($value,$symbol) {
		$val =  number_format(abs($value), 0)." ". $symbol ;
		if($this->value < 0) return "($val)";
		else return $val;
	}
	/**
	 * Lay duong dan cá»§a Page
	 *
	 * @param unknown_type $ClassName
	 */
	public function getURLSegmentByClassName($ClassName = null){
		$callerClass = "SiteTree";
		$Locale=Translatable::get_current_locale();
		$filter="ClassName = '$ClassName' and Locale='$Locale'";
		$doSet = DataObject :: get_one($callerClass,$filter);
		if($doSet)
		return $doSet->URLSegment;
		return false;
	}
	/**
	 * Cat chuoi HTML
	 * @param unknown_type $words
	 * @param unknown_type $limit
	 */
	public function getSummaryHTML ($words,$limit = 100){
		$m = 0;
		$addEplisis = '';
		$returnstr = '';
		$returnArray = array();
		$html = array();
		$chars = preg_split('/(<[^>]*[^\/]>| )/i',$words, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
		foreach ($chars as $elemnt) {
			// found start tag
			if(preg_match('/^<(p|h1|h2|h3|h4|h5|h6|q|b|i|strong|em)(.*)>$/', $elemnt)){
				preg_match('/^<(p|h1|h2|h3|h4|h5|h6|q|b|i|strong|em)(.*)>$/', $elemnt, $matches);
				array_push($html, $matches[1]);// convert <p class=""> to p
				array_push($returnArray, $elemnt);
				// found end tag
			} else if(preg_match('/^<\/(p|h1|h2|h3|h4|h5|h6|q|b|i|strong|em)(.*)>$/', $elemnt)){
				preg_match('/^<\/(p|h1|h2|h3|h4|h5|h6|q|b|i|strong|em)(.*)>$/', $elemnt, $matches);
				$testelement = array_pop ($html);
				// match (ie: <p>etc</p>)
				if($testelement==$elemnt[1]) array_pop($html);
				array_push($returnArray, $elemnt);
			} else {
				// done
				if($elemnt == ' ') continue;
				array_push($returnArray, $elemnt);
				$m++;
				if($m > $limit) {
					$addEplisis = '&hellip;';
					break;
				}
			}
		}
		// convert start tags to end tags
		$tmpr = '';
		foreach ($html as $elemnt) {
			$tmpr.='</'.$elemnt.'>';
		}
		return implode($returnArray, ' ') . $addEplisis . $tmpr;
	}
	function convert2Alias($cs)
	{
		/*Máº£ng chá»©a táº¥t cáº£ kÃ½ tá»± cÃ³ dáº¥u trong Tiáº¿ng Viá»‡t*/
		$marTViet=array("Ã ","Ã¡","áº¡","áº£","Ã£","Ã¢","áº§","áº¥","áº­","áº©","áº«","Äƒ",
			"áº±","áº¯","áº·","áº³","áºµ","Ã¨","Ã©","áº¹","áº»","áº½","Ãª","á»",
			"áº¿","á»‡","á»ƒ","á»…",
			"Ã¬","Ã­","á»‹","á»‰","Ä©",
			"Ã²","Ã³","á»","á»","Ãµ","Ã´","á»“","á»‘","á»™","á»•","á»—","Æ¡",
			"á»","á»›","á»£","á»Ÿ","á»¡",
			"Ã¹","Ãº","á»¥","á»§","Å©","Æ°","á»«","á»©","á»±","á»­","á»¯",
			"á»³","Ã½","á»µ","á»·","á»¹",
			"Ä‘",
			"Ã€","Ã","áº ","áº¢","Ãƒ","Ã‚","áº¦","áº¤","áº¬","áº¨","áºª","Ä‚",
			"áº°","áº®","áº¶","áº²","áº´",
			"Ãˆ","Ã‰","áº¸","áºº","áº¼","ÃŠ","á»€","áº¾","á»†","á»‚","á»„",
			"ÃŒ","Ã","á»Š","á»ˆ","Ä¨",
			"Ã’","Ã“","á»Œ","á»Ž","Ã•","Ã”","á»’","á»","á»˜","á»”","á»–","Æ ","á»œ","á»š","á»¢","á»ž","á» ",
			"Ã™","Ãš","á»¤","á»¦","Å¨","Æ¯","á»ª","á»¨","á»°","á»¬","á»®",
			"á»²","Ã","á»´","á»¶","á»¸",
			"Ä");

		/*Máº£ng chá»©a táº¥t cáº£ kÃ½ tá»± khÃ´ng dáº¥u tÆ°Æ¡ng á»©ng vá»›i máº£ng $marTViet bÃªn trÃªn*/
		$marKoDau=array("a","a","a","a","a","a","a","a","a","a","a",
			"a","a","a","a","a","a",
			"e","e","e","e","e","e","e","e","e","e","e",
			"i","i","i","i","i",
			"o","o","o","o","o","o","o","o","o","o","o","o",
			"o","o","o","o","o",
			"u","u","u","u","u","u","u","u","u","u","u",
			"y","y","y","y","y",
			"d",
			"A","A","A","A","A","A","A","A","A","A","A","A",
			"A","A","A","A","A",
			"E","E","E","E","E","E","E","E","E","E","E",
			"I","I","I","I","I",
			"O","O","O","O","O","O","O","O","O","O","O","O","O","O","O","O","O",
			"U","U","U","U","U","U","U","U","U","U","U",
			"Y","Y","Y","Y","Y",
			"D");

		/*HÃ m thay tháº¿ cÃ¡c kÃ­ tá»± Tiáº¿ng Viá»‡t trong máº£ng $marTViet báº±ng cÃ¡c kÃ½ tá»± khÃ´ng dáº¥u trong máº£ng $marKoDau*/
		return str_replace($marTViet,$marKoDau,$cs);
	}
}
